package com.modudf.web.servlet.maskInfoServlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.modudf.common.XML2BeanUtils;
import com.modudf.domain.MaskInfo;
import com.modudf.domain.ResultInfo;
import com.modudf.exception.XMLConvertException;
import com.modudf.service.MaskInfoManagerImpl;

public class UpdateMaskInfoServlet extends HttpServlet {

	private static final long serialVersionUID = 5896669913952204624L;
	private static final Log log = LogFactory.getLog(UpdateMaskInfoServlet.class);

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String mid = request.getParameter("mid");
		String owner = request.getParameter("owner");
		String tabName = request.getParameter("tabName");
		String tabColumn = request.getParameter("tabColumn");
		String funcName = request.getParameter("funcName");
		String funcParam = request.getParameter("funcParam");
		String funcComment = request.getParameter("funcComment");
		ResultInfo result;
		if(mid == null || mid.length() == 0) {
			result = new ResultInfo(-1, "id不能为空");
		} else {
			try{
				MaskInfo maskInfo = createMaskInfo(Integer.parseInt(mid), owner, tabName, tabColumn, funcName, funcParam, funcComment);
				MaskInfoManagerImpl.getInstance().updateMaskInfo(maskInfo);
				result = new ResultInfo();
			}catch (Exception e) {
				result = new ResultInfo(-1, e.getMessage());
				log.fatal(e);
			}
		}
		PrintWriter out = response.getWriter();
		try {
			out.println(XML2BeanUtils.bean2XmlString(result));
		} catch (XMLConvertException e) {
			e.printStackTrace();
		}
		out.close();
	}
	
	private MaskInfo createMaskInfo(int mid, String owner, String tabName, String tabColumn, String funcName, String funcParam, String funcComment) {
		MaskInfo maskInfo = new MaskInfo();
		maskInfo.setMid(mid);
		if (owner != null && owner.length() > 0) {
			maskInfo.setOwner(owner);
		}
		if (tabName != null && tabName.length() > 0) {
			maskInfo.setTabName(tabName);
		}
		if (tabColumn != null && tabColumn.length() > 0) {
			maskInfo.setTabColumn(tabColumn);
		}
		if (funcName != null && funcName.length() > 0) {
			maskInfo.setFuncName(funcName);
		}
		if (funcParam != null && funcParam.length() > 0) {
			maskInfo.setFuncParam(funcParam);
		}
		if (funcComment != null && funcComment.length() > 0) {
			maskInfo.setFuncComment(funcComment);
		}
		return maskInfo;
	}
}
